package com.ljh.SimpleTradingSystem.controller;

import com.ljh.SimpleTradingSystem.mybatis.entity.Comment;
import com.ljh.SimpleTradingSystem.mybatis.mapper.CommentMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class CommentController {
    @Resource
    CommentMapper commentMapper;

    @GetMapping("/comment")
    @ResponseBody
    public List<Comment> getComment(@RequestParam int id) {
        List<Comment> commentList = commentMapper.selectByGoodsId(id);
        return commentList;
    }

    @PostMapping("/comment")
    @ResponseBody
    public String addComment(@RequestParam int goodsId, @RequestParam String userName,
                             @RequestParam String text) {
        Date time = new Date();
        String commentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);

        Comment comment = new Comment(goodsId, userName, text, commentTime);
        commentMapper.insert(comment);
        return "success";
    }
}
